package com.uh.calculation.operator;

import com.uh.calculation.bean.FormulaBean;
import com.uh.calculation.bean.OperatorVal;
import com.uh.calculation.bean.TbFeild;
import com.uh.calculation.bean.WarnJiSuanBean;
import com.uh.calculation.util.FormulaUtil;
import com.uh.calculation.util.GetBean;
import com.uh.calculation.util.TimeUtil;
import com.uh.microservice.core.dao.JdbcDao;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 痕迹表  环比  变动算子
 */
@Data
public class HjOperator {

    private JdbcDao jdbcDao;
    private WarnJiSuanBean warnJiSuanBean;



    public  HjOperator(JdbcDao jdbcDao,WarnJiSuanBean warnJiSuanBean){
        this.jdbcDao=jdbcDao;
        this.warnJiSuanBean=warnJiSuanBean;

    }


    /**
     * 季报数据
     * 月报数据
     * @param list
     * @return
     */
    public   void jisuan(List<Map<String, Object>> ls , List<OperatorVal> list,String  feildVal ){
        if(ls == null ||  ls.size() == 0 ){
            return;
        }
        this.warnJiSuanBean=warnJiSuanBean;
        Date lastTime = warnJiSuanBean.getLastTime();

        for(OperatorVal operatorVal :list){
            List<FormulaBean> formulaBeans = operatorVal.getFormulaBeans();//计算公式
            String   field = operatorVal.getField();
            List<TbFeild> tbFeilds = GetBean.getTbf(field);
            String  f1 = tbFeilds.get(0).getFieldName();
            String  f2 = tbFeilds.get(1).getFieldName();

            String  dqval="";
            String  syhval="";

            for (int i = 1; i < ls.size(); i++) {
                String  timeCode = ls.get(i).get("EDIT_TIME").toString();
                try {
                    if (TimeUtil.bj(lastTime, timeCode, warnJiSuanBean.getYear_is_month(), warnJiSuanBean.getIs_hc())) {
                        String  META_NAME = ls.get(i).get("META_NAME").toString();
                        if(META_NAME.equals(feildVal)) {
                            if (!StringUtils.isEmpty(syhval)) {
                                //取月报数据
                                String d1 = ls.get(i).get(f1).toString();
                                //取季报数据
                                String d2 = syhval;
                                //根据当前计算类解析规则  将数据和字段对应
                                tbFeilds.get(0).setData(d1);
                                tbFeilds.get(1).setData(d2);
                                //扔进公式解析工具
                                new FormulaUtil(tbFeilds, formulaBeans, warnJiSuanBean, operatorVal.getDimension(), timeCode);
                            }
                            //赋值到上一行
                            syhval=ls.get(i).get(f1).toString();
                        }
                    }
                }catch (Exception e){
                    //e.printStackTrace();
                    syhval=ls.get(i).get(f1).toString();
                }

            }
        }
    }





}
